//
//  XJMyOrderViewController.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/5/26.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJMyOrderViewController.h"
//Controller
#import "XJAllOrderViewController.h"
#import "XJOrderWaitPayViewController.h"
#import "XJOrderWaitReceiveViewController.h"
#import "XJOrderWaitEvaluateViewController.h"
#import "XJOrderFinishViewController.h"

@interface XJMyOrderViewController ()<UIScrollViewDelegate>
@property(nonatomic, strong) UIView *titleView;
@property(nonatomic, strong) UIButton *selectedButton; // 记录选中的按钮
@property(nonatomic, strong) UIView *yellowLine; //黄线

@property(nonatomic, strong) UIScrollView *scrollView;

@end

@implementation XJMyOrderViewController

#pragma mark - lazy
- (UIView *)yellowLine {
    if (!_yellowLine) {
        _yellowLine = [UIView new];
        _yellowLine.backgroundColor = XJCOLOR_RGB(255, 218, 67);
    }
    return _yellowLine;
}

#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navTitle = @"我的订单";

    [self setupTitleView];
    
    [self addchildVCs];
    
    [self setupScrollView];
    
    [self addChildVCViews];
 
    
    //作用:想实现这样的效果:点击了待评价。对应的选中的就是待评价这个界面。
    [self clickTitleButtonAction:self.titleView.subviews[self.currentIndex]];
}


#pragma mark - 一 UI
- (void)setupTitleView {
    
    UIView *titleV = [[UIView alloc] init];
    
    // iphoneX
//    CGFloat topH = 64;
//    if (KHeight > 667) {
//        topH = 88;
//    }
    // ？基类里面写了
    titleV.frame = CGRectMake(0, 0, KWidth, 35);
    self.titleView = titleV;
    [self.view addSubview:titleV];
    
    NSArray *titleArr = @[@"全部", @"待付款", @"待收货" ,@"待评价", @"已完成"];
    CGFloat btnW = titleV.rb_width / titleArr.count;
    CGFloat btnH = titleV.rb_height;
    for (int i = 0 ; i<titleArr.count; i++) {
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        button.frame = CGRectMake(btnW * i, 0, btnW, btnH);
        [button addTarget:self action:@selector(clickTitleButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        button.tag = i;
        [button setTitle:titleArr[i] forState:(UIControlStateNormal)];
        [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [titleV addSubview:button];
    }
    
    
    //取出第一个按钮
    UIButton *firstButton = [titleV.subviews firstObject];
    
    // 黄线
    self.yellowLine.rb_width = 40.0;
    self.yellowLine.rb_height = 2;
    self.yellowLine.rb_y = titleV.rb_height - self.yellowLine.rb_height * 2;
    self.yellowLine.rb_center_x = firstButton.rb_center_x;
    [titleV addSubview:self.yellowLine];
    
    // 默认选中第一个按钮
    [self clickTitleButtonAction:firstButton];
}



- (void)clickTitleButtonAction:(UIButton *)btn {
        btn.titleLabel.font = XJFONTSET_M(18);
    self.selectedButton.selected = NO;
    btn.selected = YES;
    self.selectedButton.titleLabel.font = [UIFont systemFontOfSize:18.0];// btn 默认字体是18.00pt
    self.selectedButton = btn;
    
    //改变线条的位置
    [UIView animateWithDuration:0.25 animations:^{
        self.yellowLine.rb_center_x = btn.rb_center_x;
    }];
    
    //让scrollview滚动
    CGPoint offset = self.scrollView.contentOffset;
    offset.x = btn.tag * self.scrollView.rb_width;
    [self.scrollView setContentOffset:offset animated:YES];
}


- (void)setupScrollView {
    self.automaticallyAdjustsScrollViewInsets =  NO;

    UIScrollView *scrollV = [[UIScrollView alloc] init];
    scrollV.frame = CGRectMake(0, 35, KWidth, KHeight - 35); // 35头部标题栏
    scrollV.pagingEnabled = YES; // 可以分页
    
    scrollV.delegate = self;
    [self.view addSubview:scrollV];
    self.scrollView = scrollV;
    
    // 设置contentSize
    NSUInteger count = self.childViewControllers.count;
    XJLog(@"子控制器的个数:%ld",count);
    scrollV.contentSize = CGSizeMake(count * KWidth, 0);
}

#pragma mark - 二 添加子视图控制器 & 自控制器view
- (void)addchildVCs{
    
    //1.全部
    XJAllOrderViewController *allOrderVC = [[XJAllOrderViewController alloc] init];
    [self addChildViewController:allOrderVC];
    
    //2.待付款
    XJOrderWaitPayViewController *waitpayVC = [[XJOrderWaitPayViewController alloc] init];
    [self addChildViewController:waitpayVC];
    
    //3.待收货
    XJOrderWaitReceiveViewController *waitReceiveVC = [[XJOrderWaitReceiveViewController alloc] init];
    [self addChildViewController:waitReceiveVC];
    
    //4.待评价
    XJOrderWaitEvaluateViewController *waitEvalueteVC = [[XJOrderWaitEvaluateViewController alloc] init];
    [self addChildViewController:waitEvalueteVC];
    
    //5.已完成
    XJOrderFinishViewController *finishVC = [[XJOrderFinishViewController alloc] init];
    [self addChildViewController:finishVC];
}
// 子视图控制器的view添加到scrollView上。
- (void)addChildVCViews {
    NSInteger index = self.scrollView.contentOffset.x / self.scrollView.rb_width;
    
    UIViewController *childVC = self.childViewControllers[index];
    if ([childVC isViewLoaded]) {return;}
    
    childVC.view.frame = self.scrollView.bounds;
    [self.scrollView addSubview:childVC.view];
}

#pragma mark - 三 <UIScrollViewDelegate>
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    [self addChildVCViews];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    NSInteger index = scrollView.contentOffset.x / scrollView.rb_width;
    
    UIButton * btn = self.titleView.subviews[index];
    
    [self clickTitleButtonAction:btn];//切换按钮状态
    
    [self addChildVCViews];// 切换view
}





@end
